package quartz.demo1;

import java.util.Hashtable;
import java.util.Map;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

/**
 * 定时任务管理类
 * @ClassName: QuartzManager
 */
public class QuartzManager {
	private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
	
	public static Scheduler getScheduler() throws Exception {
		return gSchedulerFactory.getScheduler();
	}
	
	/**
	 * 获取触发器
	 * @param jobName 任务名
	 * @param triggerGroupName 触发器组
	 * @return 触发器
	 * @throws Exception
	 */
	public static Trigger getTrigger(String jobName, String triggerGroupName) throws Exception {
		Scheduler scheduler = gSchedulerFactory.getScheduler();
		return scheduler.getTrigger(new TriggerKey(jobName, triggerGroupName));
	}
	
	/**
	 * 获取任务
	 * @param jobName 任务名
	 * @param jobGroupName 任务组
	 * @return 任务
	 * @throws Exception
	 */
	public static JobDetail getJob(String jobName, String jobGroupName) throws Exception {
		Scheduler scheduler = gSchedulerFactory.getScheduler();
		JobKey jobKey = new JobKey(jobName, jobGroupName);
		return scheduler.getJobDetail(jobKey);
	}
	
	/**
	 * 暂停任务
	 * @param jobName 任务名
	 * @param jobGroupName 任务组名
	 * @throws Exception
	 */
	public static void pauseJob(String jobName, String jobGroupName) throws Exception {
		QuartzManager.getScheduler().pauseJob(new JobKey(jobName, jobGroupName));
	}
	
	public static void startJob(String jobName, String jobGroupName) throws Exception {
		QuartzManager.getScheduler().resumeJob(new JobKey(jobName, jobGroupName));
	}

	/**
	 * 添加一个定时任务，使用默认的任务组名，触发器名，触发器组名
	 * @param jobName
	 *            任务名
	 * @param cls
	 *            任务
	 * @param time
	 *            时间设置，参考quartz说明文档
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void addJob(String jobName,String jobGroupName, String triggerGroupName, Class cls, String time, String requestUrl) throws Exception {
		Scheduler sched = gSchedulerFactory.getScheduler();
		// 触发器
		CronTriggerImpl trigger = new CronTriggerImpl();// 触发器名,触发器组
		trigger.setName(jobName);
		trigger.setGroup(triggerGroupName);
		trigger.setCronExpression(time);// 触发器时间设定
		
		JobDetailImpl jobDetail = new JobDetailImpl();// 任务名，任务组，任务执行类
		jobDetail.setName(jobName);
		jobDetail.setGroup(jobGroupName);
		jobDetail.setJobClass(cls);
		Map<String, Object> jobData = new Hashtable<String, Object>();
		jobData.put("requestTimes", 0);
		jobData.put("requestUrl", requestUrl);
		RequestJobUrl.jobRunningData.put(jobName+jobGroupName, jobData);
		sched.scheduleJob(jobDetail, trigger);
		// 启动
		if (!sched.isShutdown()) {
			sched.start();
		}
	}

	/**
	 * 修改一个任务的触发时间(使用默认的任务组名，触发器名，触发器组名)
	 * @param jobName
	 * @param time
	 */
	public static void modifyJobTime(String jobName, String jobGroupName, String triggerGroupName, String time, String requestUrl) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			TriggerKey triggerKey = new TriggerKey(jobName, triggerGroupName);
			CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
			if (trigger == null) {
				return;
			}
			String oldTime = trigger.getCronExpression();
			if (!oldTime.equalsIgnoreCase(time)) {
				JobKey jobKey = new JobKey(jobName, jobGroupName);
				JobDetail jobDetail = sched.getJobDetail(jobKey);
				Class<? extends Job> objJobClass = jobDetail.getJobClass();
				removeJob(jobName, jobGroupName, triggerGroupName);
				addJob(jobName, jobGroupName, triggerGroupName, objJobClass, time, requestUrl);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 移除一个任务(使用默认的任务组名，触发器名，触发器组名)
	 * @param jobName
	 */
	public static void removeJob(String jobName, String jobGroupName, String triggerGroupName) throws Exception {
		Scheduler sched = gSchedulerFactory.getScheduler();
		TriggerKey triggerKey = new TriggerKey(jobName, triggerGroupName);
		sched.pauseTrigger(triggerKey);// 停止触发器
		sched.unscheduleJob(triggerKey);// 移除触发器
		JobKey jobKey = new JobKey(jobName, jobGroupName);
		sched.deleteJob(jobKey);// 删除任务
	}

	/**
	 * 启动所有定时任务
	 */
	public static void startJobs() {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			sched.start();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 关闭所有定时任务
	 */
	public static void shutdownJobs() {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			if (!sched.isShutdown()) {
				sched.shutdown();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
